#
#-----[ SQL ]------------------------------------------
#
INSERT INTO `phpbb_config` VALUES ('dl_statuses_perms', 'auth_admin');

CREATE TABLE `phpbb_dl_st_prevs_list` (
  `prevs_id` mediumint(8) unsigned NOT NULL auto_increment,
  `prevs_user_pg` mediumint(8) NOT NULL default '0',
  `prevs_user_png` mediumint(8) NOT NULL default '0',
  `prevs_group_g` mediumint(8) NOT NULL default '0',
  `prevs_until` int(11) NOT NULL default '0',
  PRIMARY KEY  (`prevs_id`),
  KEY `prevs_user_pg_user_png_group_g` (`prevs_user_pg`,`prevs_user_png`,`prevs_group_g`)
);

#
#-----[ COPY ]------------------------------------------
#
copy root/admin/admin_dl_statuses.php to admin/admin_dl_statuses.php
copy root/templates/subSilver/admin/admin_dl_statuses.tpl to templates/subSilver/admin/admin_dl_statuses.tpl

#
#-----[ OPEN ]------------------------------------------
#
includes/constants.php

#
#-----[ FIND ]------------------------------------------
#
// Table names

#
#-----[ AFTER, ADD ]------------------------------------------
#
define('DOWNLOAD_STATUSES_TBL', $table_prefix.'dl_st_prevs_list');


#
#-----[ OPEN ]------------------------------------------
#
includes/functions.php

#
#-----[ FIND ]------------------------------------------
#
?>

#
#-----[ BEFORE, ADD ]------------------------------------------
#
function get_groupdata($group, $force_str = false)
{
	global $db;

	if (!is_numeric($group) || $force_str)
	{
		$group = phpbb_clean_username($group);
	}
	else
	{
		$group = intval($group);
	}

	$sql = "SELECT *
		FROM " . GROUPS_TABLE . " 
		WHERE ";
	$sql .= ( ( is_integer($group) ) ? "group_id = $group" : "group_name = '" .  str_replace("\'", "''", $group) . "'" ) . " AND group_id <> 0";
	if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Tried obtaining data for a non-existent group', '', __LINE__, __FILE__, $sql);
	}

	// Start Advanced IP Tools Pack MOD
	if ( $db->sql_affectedrows() == 0 )
	{
		message_die(GENERAL_ERROR, 'Group does not exist.');
	}
	// End Advanced IP Tools Pack MOD

	return ( $row = $db->sql_fetchrow($result) ) ? $row : false;
}

function prune_dl_st_prevs_list()
{
	global $db;

	$sql = "DELETE FROM " . DOWNLOAD_STATUSES_TBL . "
		WHERE prevs_until <> 0 AND prevs_until < " . time();
	if (!($result = $db->sql_query($sql)))
	{
		message_die(GENERAL_ERROR, 'Could not access prevslist', '', __LINE__, __FILE__, $sql);
	}
}

function check_prevs_g($user_id)
{
	global $db;


	$sql_in = '';
	$sql = "select group_id
		from ".USER_GROUP_TABLE."
		where user_id=$user_id
		and user_pending=0";
	$result = $db->sql_query($sql);
	while ($row = $db->sql_fetchrow($result) )
	{
		$sql_in .= ( ( $sql_in != '' ) ? ', ' : '' ) . $row['group_id'];
	}
	$db->sql_freeresult($result);
	if ($sql_in!= '')
	{
		$sql = "SELECT * 
			FROM " . DOWNLOAD_STATUSES_TBL . "
			WHERE prevs_group_g IN ($sql_in)";
		if (!($result = $db->sql_query($sql)))
		{
			message_die(GENERAL_ERROR, 'Could not access banlist', '', __LINE__, __FILE__, $sql);
		}
		if ($db->sql_fetchrow($result))
		{
			return true;		
		}
		$db->sql_freeresult($result);
	}

	$sql = "SELECT * 
		FROM " . DOWNLOAD_STATUSES_TBL . "
		WHERE prevs_user_pg =$user_id";
	if (!($result = $db->sql_query($sql))) message_die(GENERAL_ERROR, 'Could not access banlist', '', __LINE__, __FILE__, $sql);
	if ($db->sql_fetchrow($result))	{
		return true;		
	}
	return false;
}

function check_prevs_png($user_id)
{
	global $db;

	$sql = "SELECT * 
		FROM " . DOWNLOAD_STATUSES_TBL . "
		WHERE prevs_user_png =$user_id";
	if (!($result = $db->sql_query($sql))) message_die(GENERAL_ERROR, 'Could not access banlist', '', __LINE__, __FILE__, $sql);

	if ($db->sql_fetchrow($result))	{
		return true;		
	}else{ return false; }
}

#
#-----[ OPEN ]------------------------------------------
#
posting.php

#
#-----[ FIND ]------------------------------------------
#
//
// End session management
//

#
#-----[ AFTER, ADD ]------------------------------------------
#
prune_dl_st_prevs_list();

#
#-----[ FIND ]------------------------------------------
#
				$topic_type_gold = ( ($topic_type_gold != $post_data['topic_type_gold']) && $is_auth['auth_mod'] ) ? $topic_type_gold : $post_data['topic_type_gold'];

#
#-----[ REPLACE WITH ]------------------------------------------
#
				$topic_type_gold = ( ($topic_type_gold != $post_data['topic_type_gold']) && ($is_auth[$board_config['dl_statuses_perms']] || check_prevs_g($userdata['user_id']) && (!check_prevs_png($userdata['user_id']))) ) ? $topic_type_gold : $post_data['topic_type_gold'];

#
#-----[ FIND ]------------------------------------------
#
	if( $is_auth['auth_mod'] )

#
#-----[ REPLACE WITH ]------------------------------------------
#
	if( ($is_auth[$board_config['dl_statuses_perms']] || check_prevs_g($userdata['user_id'])) && (!check_prevs_png($userdata['user_id'])) )

#
#-----[ OPEN ]------------------------------------------
#
attach_mod/displaying_torrent.php

#
#-----[ FIND ]------------------------------------------
#
	$template->assign_block_vars('postrow.attach.tor_reged', array(

#
#-----[ BEFORE, ADD ]------------------------------------------
#
	 switch ($forum_topic_data['topic_type_gold'])
	         {
	         case 1: $topic_type_gold='<img src="images/gold.gif" alt=" "> '; break; 
	         case 2: $topic_type_gold='<img src="images/silver.gif" alt=" "> '; break; 
	         case 3: $topic_type_gold='<img src="images/bronze.gif" alt=" "> '; break; 
	         default: $topic_type_gold='';
	         }

#
#-----[ FIND ]------------------------------------------
#
  		'S_UPLOAD_IMAGE'      => $upload_image,

#
#-----[ AFTER, ADD ]------------------------------------------
#
		'TOPIC_TYPE_GOLD'     => $topic_type_gold,

#
#-----[ OPEN ]------------------------------------------
#
templates/SubSilver/viewtopic_attach_body.tpl

#
#-----[ FIND ]------------------------------------------
#
<td width="10%" class="attachrow" rowspan="6" align="center" style="padding: 5px" nowrap="nowrap">{postrow.attach.tor_reged.S_UPLOAD_IMAGE}<b>{postrow.attach.tor_reged.L_DOWNLOADEDT}</b>

#
#-----[ IN-LINE FIND ]------------------------------------------
#
{postrow.attach.tor_reged.FILESIZE}

#
#-----[ IN-LINE AFTER ADD ]------------------------------------------
#
<br /><br />{postrow.attach.tor_reged.TOPIC_TYPE_GOLD}


#
#-----[ OPEN ]------------------------------------------
#
tracker.php

#
#-----[ FIND ]------------------------------------------
#
$tor_colspan         = 13;   // colspan with all columns

#
#-----[ REPLACE WITH ]------------------------------------------
#
$tor_colspan         = 14;   // colspan with all columns

#
#-----[ FIND ]------------------------------------------
#
$tor_list_sql = $tor_to_show_sql = $opt = $order_options = $time_options = $s_not_seen_options = '';

#
#-----[ REPLACE WITH ]------------------------------------------
#
$tor_list_sql = $tor_to_show_sql = $opt = $order_options = $time_options = $s_not_seen_options = $s_dl_status_options = '';

#
#-----[ FIND ]------------------------------------------
#
$s_not_seen_key  = 'sns';

#
#-----[ AFTER, ADD ]------------------------------------------
#
$s_dl_status_key  = 'sds';

#
#-----[ FIND ]------------------------------------------
#
// Seeder not seen
$s_not_seen_opt = array(
        $search_all => array(
                     'lang' => $lang['Bt_Disregard'],
                     'sql'  => 0
                    ),
        $never  => array(
                     'lang' => $lang['Bt_Never'],
                     'sql'  => 0
                    ),
        30 => array(
                     'lang' => $lang['Bt_1_Month'],
                     'sql'  => $current_time - 86400*30
                    ),
        14 => array(
                     'lang' => $lang['Bt_2_Weeks'],
                     'sql'  => $current_time - 86400*14
                    ),
        7  => array(
                     'lang' => $lang['Bt_7_Days'],
                     'sql'  => $current_time - 86400*7
                    ),
        3  => array(
                     'lang' => $lang['Bt_3_Days'],
                     'sql'  => $current_time - 86400*3
                    )
);

#
#-----[ AFTER, ADD ]------------------------------------------
#
// Download statuses
$s_dl_status_opt = array(
        $search_all => array(
                     'lang' => '  ',
                     'sql'  => 0
                    ),
        0 => array(
                     'lang' => ' ',
                     'sql'  => 0
                    ),
        1 => array(
                     'lang' => ' ',
                     'sql'  => 1
                    ),
        2  => array(
                     'lang' => ' ',
                     'sql'  => 2
                    ),
        3  => array(
                     'lang' => ' ',
                     'sql'  => 3
                    )
);

#
#-----[ FIND ]------------------------------------------
#
$s_not_seen_val  = $def_s_not_seen_val  = $search_all;

#
#-----[ AFTER, ADD ]------------------------------------------
#
$s_dl_status_val  = $def_s_dl_status_val  = $search_all;


#
#-----[ FIND ]------------------------------------------
#
	select_get_val ($s_not_seen_key, $s_not_seen_val,  $s_not_seen_opt,  $def_s_not_seen_val);

#
#-----[ AFTER, ADD ]------------------------------------------
#
        select_get_val ($s_dl_status_key, $s_dl_status_val,  $s_dl_status_opt,  $def_s_dl_status_val);

#
#-----[ FIND ]------------------------------------------
#
		$s_not_seen_key  => $s_not_seen_val,
#
#-----[ AFTER, ADD ]------------------------------------------
#
                $s_dl_status_key  => $s_dl_status_val,

#
#-----[ FIND ]------------------------------------------
#
	$s_not_seen  = ($s_not_seen_val != $search_all) ? TRUE : FALSE;

#
#-----[ AFTER, ADD ]------------------------------------------
#
        $s_dl_status  = ($s_dl_status_val != $search_all) ? TRUE : FALSE;

#
#-----[ FIND ]------------------------------------------
#
                $s_seen_exl  = ($s_not_seen_val == $never) ? '' : 'AND tor.seeder_last_seen != 0';

#
#-----[ AFTER, ADD ]------------------------------------------
#
                $s_dl_status_v = $s_dl_status_opt[$s_dl_status_val]['sql'];

#
#-----[ FIND ]------------------------------------------
#
		$where_sql .= ($s_not_seen) ? "
				AND tor.seeder_last_seen $s_seen_sign $s_seen_time
				$s_seen_exl
		" :	'';

#
#-----[ AFTER, ADD ]------------------------------------------
#
                $where_sql .= ($s_dl_status) ? "
                                AND t.topic_type_gold=$s_dl_status_v
                " :     '';

#
#-----[ FIND ]------------------------------------------
#
				$compl  = $tor_row['complete_count'] + $tor_row['complete_count_guest'];

#
#-----[ AFTER, ADD ]------------------------------------------
#
                                 switch ($tor_row['topic_type_gold'])
                                         {
                                         case 1: $topic_type_gold='<img src="images/gold.gif"> '; $l_status=' '; break; 
                                         case 2: $topic_type_gold='<img src="images/silver.gif"> '; $l_status=' '; break; 
                                         case 3: $topic_type_gold='<img src="images/bronze.gif"> '; $l_status='  '; break; 
                                         default: $topic_type_gold='<span style=color:white;> - <span>'; $l_status=' '; break; 
                                         }

#
#-----[ FIND ]------------------------------------------
#
					'COMPLETED'    => ($compl) ? $compl : 0,
#
#-----[ AFTER, ADD ]------------------------------------------
#
                                        'DL_STATUS'    => $topic_type_gold,
                                        'L_DL_STATUS'    => $l_status,
#
#-----[ FIND ]------------------------------------------
#
// Seeder not seen
foreach ($s_not_seen_opt as $val => $opt)
{
        $selected = ($val == $s_not_seen_val) ? $sl : '';
        $s_not_seen_options .= '<option value="'. $val .'" '. $selected .'>&nbsp;'. $opt['lang'] .'&nbsp;</option>';
}

#
#-----[ AFTER, ADD ]------------------------------------------
#
// Download statuses
foreach ($s_dl_status_opt as $val => $opt)
{
        $selected = ($val == $s_dl_status_val) ? $sl : '';
        $s_dl_status_options .= '<option value="'. $val .'" '. $selected .'>&nbsp;'. $opt['lang'] .'&nbsp;</option>';
}

#
#-----[ FIND ]------------------------------------------
#
	'S_NOT_SEEN_NAME'     => $s_not_seen_key,

#
#-----[ AFTER, ADD ]------------------------------------------
#
        'S_DL_STATUS_NAME'    => $s_dl_status_key,
        'S_DL_STATUS_VAL'     => $s_dl_status_val,

#
#-----[ FIND ]------------------------------------------
#
	'S_NOT_SEEN_OPTIONS' => $s_not_seen_options,

#
#-----[ AFTER, ADD ]------------------------------------------
#
        'S_DL_STATUS_OPTIONS' => $s_dl_status_options,

#
#-----[ OPEN ]------------------------------------------
#
templates/SubSilver/tracker.tpl

#
#-----[ FIND ]------------------------------------------
#
					<td valign="top" class="row1" nowrap="nowrap">
						<fieldset class="fieldset" style="margin: 2px;">
							<legend>{L_SEED_NOT_SEEN}</legend>
							<div style="padding: 4px">
								&nbsp;<select class="post" name="{S_NOT_SEEN_NAME}">{S_NOT_SEEN_OPTIONS}</select>&nbsp;
							</div>
						</fieldset>
					</td>
#
#-----[ AFTER, ADD ]------------------------------------------
#
					<td valign="top" class="row1" nowrap="nowrap">
						<fieldset class="fieldset" style="margin: 2px;">
							<legend> </legend>
							<div style="padding: 4px">
								&nbsp;<select class="post" name="{S_DL_STATUS_NAME}">{S_DL_STATUS_OPTIONS}</select>&nbsp;
							</div>
						</fieldset>
					</td>

#
#-----[ FIND ]------------------------------------------
#
		<th align="center" nowrap="nowrap" title="{L_SIZE}"> {L_SHORT_Size} </th>

#
#-----[ AFTER, ADD ]------------------------------------------
#
		<th align="center" nowrap="nowrap" title="">  </th>

#
#-----[ FIND ]------------------------------------------
#
		<td align="center" title="{tor.TITLE_SPEED}" nowrap="nowrap" class="gensmall">{tor.TOR_SIZE}</td>

#
#-----[ AFTER, ADD ]------------------------------------------
#
		<td align="center" title="{tor.L_DL_STATUS}" class="gensmall">{tor.DL_STATUS}</td>

#
#-----[ OPEN ]------------------------------------------
#
search.php

#
#-----[ FIND ]------------------------------------------
#
if ( $mode == 'searchuser' )

#
#-----[ BEFORE ADD ]------------------------------------------
#
if ( $mode == 'searchgroup' )
{
	//
	// This handles the simple windowed user search functions called from various other scripts
	//

	if ( isset($HTTP_POST_VARS['forma']) || isset($HTTP_GET_VARS['forma']))
	{
		$forma = ( isset($HTTP_POST_VARS['forma']) ) ? $HTTP_POST_VARS['forma'] : $HTTP_GET_VARS['forma'];
		$forma = (in_array($forma, array('group_g'))) ? $forma : '';
	}


	if ( isset($HTTP_POST_VARS['search_username']) )
	{
		groupname_search($HTTP_POST_VARS['search_username'],$forma);
	}
	else
	{
		groupname_search('',$forma);
	}

	exit;
}

#
#-----[ FIND ]------------------------------------------
#
if ( $mode == 'searchuser' )
{
	//
	// This handles the simple windowed user search functions called from various other scripts
	//
	if ( isset($HTTP_POST_VARS['search_username']) )
	{
		username_search($HTTP_POST_VARS['search_username']);
	}
	else
	{
		username_search('');
	}

	exit;
}
#
#-----[ REPLACE WITH ]------------------------------------------
#
if ( $mode == 'searchuser' )
{
	//
	// This handles the simple windowed user search functions called from various other scripts
	//
	if ( isset($HTTP_POST_VARS['forma']) || isset($HTTP_GET_VARS['forma']))
	{
		$forma = ( isset($HTTP_POST_VARS['forma']) ) ? $HTTP_POST_VARS['forma'] : $HTTP_GET_VARS['forma'];
		$forma = (in_array($forma, array('username_pg', 'group_g', 'username_png'))) ? $forma : '';
	}

	if ( isset($HTTP_POST_VARS['search_username']) )
	{
		username_search($HTTP_POST_VARS['search_username'], $forma);
	}
	else
	{
		username_search('', $forma);
	}

	exit;
}

#
#-----[ OPEN ]------------------------------------------
#
includes/functions_search.php

#
#-----[ FIND ]------------------------------------------
#
//
// Username search
//
#
#-----[ BEORE ADD ]------------------------------------------
#
//
// Groupname search
//
function groupname_search($search_match, $forma)
{
	global $db, $board_config, $template, $lang, $images, $theme, $phpEx, $phpbb_root_path;
	global $starttime, $gen_simple_header;
	
	$gen_simple_header = TRUE;

	$groupname_list = '';
	if ( !empty($search_match) )
	{
		$groupname_search = preg_replace('/\*/', '%', phpbb_clean_username($search_match));

		$sql = "SELECT group_name 
			FROM " . GROUPS_TABLE . " 
			WHERE group_name LIKE '" . str_replace("\'", "''", $groupname_search) . "' AND 'group_name' <> ''
			ORDER BY group_name";
		if ( !($result = $db->sql_query($sql)) )
		{
			message_die(GENERAL_ERROR, 'Could not obtain search results', '', __LINE__, __FILE__, $sql);
		}

		if ( $row = $db->sql_fetchrow($result) )
		{
			do
			{
				$groupname_list .= '<option value="' . $row['group_name'] . '">' . $row['group_name'] . '</option>';
			}
			while ( $row = $db->sql_fetchrow($result) );
		}
		else
		{
			$groupname_list .= '<option>' . $lang['No_match']. '</option>';
		}
		$db->sql_freeresult($result);
	}

	$page_title = $lang['Search'];
	include($phpbb_root_path . 'includes/page_header.'.$phpEx);

	$template->set_filenames(array(
		'search_user_body' => 'search_username.tpl')
	);

	$template->assign_vars(array(
		'USERNAME' => (!empty($search_match)) ? phpbb_clean_username($search_match) : '', 
		'USERNAME_PG' => (!empty($search_match)) ? phpbb_clean_username($search_match) : '', 


		'L_CLOSE_WINDOW' => $lang['Close_window'], 
		'L_SEARCH_USERNAME' => ' ', 
		'L_UPDATE_USERNAME' => $lang['Select_username'], 
		'L_SELECT' => $lang['Select'], 
		'L_SEARCH' => $lang['Search'], 
		'L_SEARCH_EXPLAIN' => $lang['Search_author_explain'], 
		'L_CLOSE_WINDOW' => $lang['Close_window'], 
		'FORMA' => ($forma) ? $forma:'username', 

		'S_USERNAME_OPTIONS' => $groupname_list, 
		'S_SEARCH_ACTION' => append_sid("search.$phpEx?mode=searchgroup&forma=$forma"))
	);

	if ( $groupname_list != '' )
	{
		$template->assign_block_vars('switch_select_name', array());
	}

	$template->pparse('search_user_body');

	include($phpbb_root_path . 'includes/page_tail.'.$phpEx);

	return;
}
#
#-----[ FIND ]------------------------------------------
#
function username_search($search_match)

#
#-----[ REPLACE WITH ]------------------------------------------
#
function username_search($search_match, $forma)

#
#-----[ FIND ]------------------------------------------
#
		'L_CLOSE_WINDOW' => $lang['Close_window'], 
#
#-----[ AFTER ADD ]------------------------------------------
#
		'FORMA' => ($forma) ? $forma:'username', 
#
#-----[ FIND ]------------------------------------------
#
		'S_SEARCH_ACTION' => append_sid("search.$phpEx?mode=searchuser"))

#
#-----[ REPLACE WITH ]------------------------------------------
#
		'S_SEARCH_ACTION' => append_sid("search.$phpEx?mode=searchuser&forma=$forma"))
#
#-----[ OPEN ]------------------------------------------
#
templates/SubSilver/search_username.tpl

#
#-----[ FIND ]------------------------------------------
#
	opener.document.forms['post'].username.value = selected_username;

#
#-----[ REPLACE WITH ]------------------------------------------
#
	opener.document.forms['post'].{FORMA}.value = selected_username;
# 
#-----[ SAVE/CLOSE ALL FILES ]------------------------------------------ 
# 
# EoM